package com.mmy.controller.restaurant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.restaurant.TRestaurantPlan;
import com.mmy.domain.restaurant.VRestaurantBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.restaurant.TRestaurantBaseServiceImpl;
import com.mmy.service.impl.restaurant.TRestaurantPlanServiceImpl;
import com.mmy.service.impl.restaurant.VRestaurantBaseServiceImpl;

/**
 * 类的介绍：车公司信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-11
 * @version 1.0
 */
@Controller
public class TRestaurantPlanController {

	@Autowired
	TRestaurantPlanServiceImpl tRestaurantPlanServiceImpl;
	@Autowired
	TRestaurantBaseServiceImpl tRestaurantBaseServiceImpl;
	@Autowired
	VRestaurantBaseServiceImpl vRestaurantBaseServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-11
	 * 
	 * 
	 */
	/* 进入创建餐馆游计划单页面 */
	@RequestMapping(value = "/tocreaterestaurantplan")
	public String toCreatRestaurantPlan(Model model) {
		/* 查询餐馆信息 */
		List<VRestaurantBase> lstRestaurantBase = vRestaurantBaseServiceImpl.findAll();
		model.addAttribute("newLstRestaurantBase", lstRestaurantBase);

		return "webbackground/restaurantmanagement/createrestaurantplan";
	}

	/**
	 * 函数功能说明： 新建餐馆计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/subcreaterestaurantplan")
	public String subCreateRestaurantPlan(TRestaurantPlan tRestaurantPlan, String startTime, String endTime,
			HttpSession session) throws Exception {

		tRestaurantPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tRestaurantPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));

		// 获取餐馆游计划单最大值
		tRestaurantPlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_RESTAURANT_PLAN));

		try {

			tRestaurantPlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tRestaurantPlan.setCreator(tSysUser.getUserName());
			tRestaurantPlanServiceImpl.save(tRestaurantPlan.getPlanID());
			tRestaurantPlanServiceImpl.save(tRestaurantPlan);
			return "redirect:restaurantplan";

		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/saveupdaterestaurantplan")
	public String saveUpdateRestaurantPlan(TRestaurantPlan tRestaurantPlan, String startTime, String endTime,
			HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TRestaurantPlan dtRestaurantPlan = tRestaurantPlanServiceImpl.findByPlanID(tRestaurantPlan.getPlanID());
			dtRestaurantPlan.setService(tRestaurantPlan.getService());
			dtRestaurantPlan.setRestaurantID(tRestaurantPlan.getRestaurantID());
			dtRestaurantPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtRestaurantPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtRestaurantPlan.setFloor(tRestaurantPlan.getFloor());
			dtRestaurantPlan.setPeer(tRestaurantPlan.getPeer());
			dtRestaurantPlan.setPrice(tRestaurantPlan.getPrice());
			dtRestaurantPlan.setNumber(tRestaurantPlan.getNumber());
			dtRestaurantPlan.setMemo(tRestaurantPlan.getMemo());
			dtRestaurantPlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtRestaurantPlan.setReviser(tSysUser.getUserName());
			tRestaurantPlanServiceImpl.save(dtRestaurantPlan);

			return "redirect:restaurantplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除餐馆计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/deleterestaurantplanchecked")
	@ResponseBody
	public String deleteRestaurantPlanChecked(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		tRestaurantPlanServiceImpl.delRestaurantPlan(lstPlanID);

		return "webbackground/restaurantmanagement/restaurantplan";
	}

}
